﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MsTestUtilities.SolutionAnalysis;
using MsTestUtilities.RowTests;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;

namespace MsTestUtilitiesTests.SolutionAnalysisTests
{
    [TestClass]
    public class ItemGroupClassifierTests
    {
        private ItemGroupClassifier _classifier;

        [TestInitialize]
        public void Setup()
        {
            _classifier = new ItemGroupClassifier(null);
        }

        [TestMethod]
        public void ReferenceTest()
        {
            AddItem("Reference");            
            
            AssertGroupHasItem(_classifier.ItemGroups.Reference);
        }

        [TestMethod]
        public void ProjectReferenceTest()
        {
            AddItem("ProjectReference");

            AssertGroupHasItem(_classifier.ItemGroups.ProjectReference);
        }

        [TestMethod]
        public void ContentFileTest()
        {
            AddItem("Content");

            AssertGroupHasItem(_classifier.ItemGroups.Content);
        }

        [TestMethod]
        public void UnknownItemTest()
        {
            AddItem("Sludder");

            AssertGroupHasItem(_classifier.ItemGroups.Unknown);
        }

        private void AddItem(string name)
        {
            var item = CreateItem(name);
            _classifier.Add(item); 
        }

        private Item CreateItem(string name)
        {
            var xml = string.Format(@"<{0} Include=""System.Core"" />", name);
            var element = XElement.Parse(xml);
            return new Item(null, element);
        }

        private void AssertGroupHasItem(IEnumerable<IItem> group)
        {
            Assert.AreEqual(1, group.Count());
        }
    }
}
